package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Controller
@RestController
@Api(tags = "菜品相关接口")
@RequestMapping("/admin/dish")
public class DishContorller {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        boolean b = dishService.saveWithFlavor(dishDTO);//后绪步骤开发

        //清理缓存
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);//后绪步骤定义
        return Result.success(pageResult);
    }

    /**
     * 菜品删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品删除单个及批量")
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("要删除的字段是{}",ids);
        if (Objects.isNull(ids)) {
            return Result.error("无法删除空字段");
        }
        boolean b = dishService.deleteDish(ids);
        if (b) {
            //清理缓存
            cleanCache("dish_*");
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 修改菜品之前的菜品回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("修改菜品之前的查询准备")
    public Result<DishVO> selectDishwithFlavorsById(@PathVariable Long id){
        DishVO dishVO = dishService.getDishwithFlavorsById(id);
        return Result.success(dishVO);
    }

    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        //清理缓存
        cleanCache("dish_*");
        return Result.success();
    }

    ///dish/list

    /**
     * 定义根据分类查询菜品的方法，为新增套餐做准备
     * @param categoryId
     * @return
     */
    @ApiOperation("定义根据分类查询菜品的方法，为新增套餐做准备")
    @GetMapping("/list")
    public Result<Dish[]> selectByCateGory(String categoryId){
        Long id = Long.valueOf(categoryId);
        Dish[] dishes = dishService.selectByCateGory(id);
        return Result.success(dishes);
    }

    /**
     * 修改状态
     * @param status
     * @return
     */
    @ApiOperation("修改菜品状态")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,Long id) {
        boolean b= dishService.updateStatus(status,id);
        if (b){
            //清理缓存

            cleanCache("dish_*");
            return Result.success();
        }
        return Result.error("修改错误");
    }

    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
